package lark

import (
	"fmt"
	"time"
)

type LarkAlert struct {
	lark *Lark

	item string

	level string
	begin time.Time
	end   time.Time

	content string

	at []string
}

func (l *Lark) NewLarkAlert(item string, level string, begin time.Time, end time.Time,
	at []string, content string) *LarkAlert {

	return &LarkAlert{
		lark:    l,
		item:    item,
		level:   level,
		begin:   begin,
		end:     end,
		at:      at,
		content: content,
	}
}

func (la *LarkAlert) GenLarkTitle(c *Content) {
	c.Title = fmt.Sprintf("%s %s告警", la.lark.service, la.item)
}

func (la *LarkAlert) GenLarkLabel(c *Content) {
	c.Content = append(c.Content, ParaItem{
		newText(fmt.Sprintf("【集群ID】%s", la.lark.cluster)),
	})

	c.Content = append(c.Content, ParaItem{
		newText(fmt.Sprintf("【实例ID】%s", la.lark.instance)),
	})

	c.Content = append(c.Content, ParaItem{
		newText(fmt.Sprintf("【告警级别】%s", la.level)),
	})

	c.Content = append(c.Content, ParaItem{
		newText(fmt.Sprintf("【告警时间】%s", time.Now().Format(formatString))),
	})

	if la.begin != time.Unix(0, 0) {
		c.Content = append(c.Content, ParaItem{
			newText(fmt.Sprintf("【开始时间】%s", la.begin.Format(formatString))),
		})
	}

	if la.end != time.Unix(0, 0) {
		c.Content = append(c.Content, ParaItem{
			newText(fmt.Sprintf("【时结束间】%s", la.end.Format(formatString))),
		})
	}
}

func (la *LarkAlert) GenLarkContent(c *Content) {
	c.Content = append(c.Content, ParaItem{
		newText("【告警内容】"),
	})

	c.Content = append(c.Content, ParaItem{
		newText(fmt.Sprintf("    %s", la.content)),
	})
}

func (la *LarkAlert) GenLarkReminder(c *Content) {
	if len(la.at) == 0 {
		return
	}

	c.Content = append(c.Content, ParaItem{
		newText("---------------------------------------"),
	})

	items := ParaItem{
		newText("请关注"),
	}

	for _, uid := range la.at {
		items = append(items, newAt(uid))
	}

	c.Content = append(c.Content, items)
}
